package com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.controller;

import com.alibaba.fastjson.JSONObject;
import com.yonyou.ucf.mdf.app.isvdxq.ReturnResult.DataResult;
import com.yonyou.ucf.mdf.app.isvdxq.common.DxqConfig;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.RFIDCodeDetail.model.ShangjiaList;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.RFIDCodeDetail.service.RfidCodeDetailService;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.service.RePickService;
import com.yonyou.ucf.mdf.app.isvdxq.utils.RequestTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = {DxqConfig.pre_apiUrl+"/repick",DxqConfig.pre_pcUrl+"/repick"})
public class RePickController {

    @Autowired
    private RePickService rePickService;
    @Autowired
    private RfidCodeDetailService rfidCodeDetailService;


    @RequestMapping("/getOrderList")
    public DataResult getOrderList(String orderCode, Integer page, Integer pageSize, HttpServletRequest request) throws Exception {

        Map<String, Object> reqParams = RequestTool.getParams(request);
        String tenant_id = (String) reqParams.get("tenant_id");
        if (pageSize == null || pageSize == 0) {
            pageSize = 10;
        }

        if (page == null || page == 0) {

            page = 1;
        }
        int pageBegin = (page - 1) * pageSize;

        DataResult dataResult = rePickService.GetRepickOrderList(orderCode, pageBegin, pageSize, tenant_id);
        int totalPage = (dataResult.getTotalSize() + pageSize - 1) / pageSize;
        dataResult.setTotalPages(totalPage);
        dataResult.setPageNum(page);
        dataResult.setPageSize(pageSize);

        return dataResult;
    }

    @RequestMapping("/getRepickDetails")
    public DataResult getRepickDetails(String orderId, HttpServletRequest request) throws Exception {
        Map<String, Object> reqParams = RequestTool.getParams(request);
        String tenant_id = (String) reqParams.get("tenant_id");

        DataResult dataResult = rePickService.GetRepickDetails(orderId, tenant_id);
        return dataResult;
    }

    /**
     * 反捡提交接口 支持多次提交
     * editlast: 2023-01-31 by chen
     * @param shangjiaList 传入shangjiaList
     * @return
     * @throws Exception
     */
    @PostMapping("/RepickStock")
    public DataResult RepickStock(String tenant_id, @RequestBody ShangjiaList shangjiaList) throws Exception {
        System.out.println(JSONObject.toJSONString(shangjiaList));
        List<String> ids = Arrays.asList(shangjiaList.getId().split(",")).stream().filter(it -> !it.isEmpty()).distinct().collect(Collectors.toList());
        String epcIds = String.join(",", ids);
        DataResult dataResult = rePickService.UpdatePickStatus(epcIds, shangjiaList.getLocationId(), shangjiaList.getOrderId(), shangjiaList.getModifier(), tenant_id);
        System.out.println(JSONObject.toJSONString(dataResult));
        return dataResult;
    }

}
